package com.fishcoco.app.yunhang.activity.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.widget.AppCompatTextView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.fishcoco.app.yunhang.R;
import com.fishcoco.app.yunhang.utils.keyboard.SoftKeyInputHidWidget;

import me.yokeyword.fragmentation.SupportActivity;

public abstract class BaseActivity extends SupportActivity implements View.OnClickListener{

    private MaterialDialog findingDialog;
    private MaterialDialog loadingDialog;
    private AppCompatTextView findingDialogMsg;
    private AppCompatTextView loadingDialogMsg;
    private View statusBarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        changeStatusColor();
        init();
        setContentView(setLayoutResourceID());
        statusBarView = findViewById(R.id.status_bar_view);
        initStatusBar();
        SoftKeyInputHidWidget.assistActivity(this);
        setUpView();
        setUpData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void changeStatusColor() {
//        BarUtils.setTranslucent(this);
//        BarUtils.setColor(this, ContextCompat.getColor(this,R.color.colorPrimary));
    }
    /**
     * 动态的设置状态栏  实现沉浸式状态栏
     */
    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//大于4.4
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
                View decorView = getWindow().getDecorView();
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
            } else {
                //4.4到5.0
                WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
                localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            }
            if (statusBarView!=null) {
                statusBarView.setVisibility(View.VISIBLE);
                //获取到状态栏的高度
                int statusHeight = SoftKeyInputHidWidget.getStatusBarHeight(this);
                //动态的设置隐藏布局的高度
                ViewGroup.LayoutParams params =  statusBarView.getLayoutParams();
                params.height = statusHeight;
                statusBarView.setLayoutParams(params);
            }
        }
    }
    protected void setUpData() {}

    /***
     * 用于在初始化View之前做一些事
     */
    private void init() {

    }

    protected void setUpView(){};

    protected abstract int setLayoutResourceID();

    protected <T extends View> T $(int id) {
        return (T) super.findViewById(id);
    }


    protected void startActivityWithoutExtras(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    protected void startActivityWithExtras(Class<?> clazz, Bundle extras) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(extras);
        startActivity(intent);
    }

    // 获取点击事件
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
//            View view = getCurrentFocus();
//            if(isHideInput(view, ev)) {
//                HideSoftInput(view.getWindowToken());
//            }
//        }
//        return super.dispatchTouchEvent(ev);
//    }
    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if(v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            return !(ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom);
        }
        return false;
    }
    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if(token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void onClick(View v) {
        if (isFastDoubleClick()) return;
    }

    private long mLastClickTime = 0;
    private boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - mLastClickTime;
        if (0 < timeD && timeD < 500) {
            return true;
        }

        mLastClickTime = time;

        return false;
    }


    protected void initClickListener(int... ids) {
        for (int id : ids) {
            $(id).setOnClickListener(this);
        }
    }
    public void toast(String msg)
    {
        ToastUtils.showShort(msg);
    }


    public void loadingDialog(String msg)
    {
        if (loadingDialog==null) {
            boolean wrapInScrollView = true;
            View contentView = View.inflate(getApplicationContext(),R.layout.dialog_loading,null);
            loadingDialogMsg = (AppCompatTextView)contentView.findViewById(R.id.tv_dialog_msg);
            loadingDialog = new MaterialDialog.Builder(getApplicationContext())
                    .title(R.string.app_name)
                    .customView(contentView, wrapInScrollView)
                    .positiveText(R.string.close)
                    .build();
        }
        if (StringUtils.isTrimEmpty(msg))
        {
            loadingDialogMsg.setText(getString(R.string.findfindfind));
        } else {
            loadingDialogMsg.setText(msg);
        }
        if (!loadingDialog.isShowing())
        {
            loadingDialog.show();
        }
    }

    public void findingDialog(String msg)
    {
        if (findingDialog==null) {
            boolean wrapInScrollView = true;
            View contentView = View.inflate(getApplicationContext(),R.layout.dialog_finding,null);
            findingDialogMsg = (AppCompatTextView)contentView.findViewById(R.id.tv_dialog_msg);
            findingDialog = new MaterialDialog.Builder(getApplicationContext())
                    .title(R.string.app_name)
                    .customView(contentView, wrapInScrollView)
                    .positiveText(R.string.close)
                    .build();
        }
        if (StringUtils.isTrimEmpty(msg))
        {
            findingDialogMsg.setText(getString(R.string.findfindfind));
        } else {
            findingDialogMsg.setText(msg);
        }
        if (!findingDialog.isShowing())
        {
            findingDialog.show();
        }
    }

    public void dialogDismiss()
    {
        if (loadingDialog!=null)
        {
            if (loadingDialog.isShowing())
            {
                loadingDialog.dismiss();
            }
        }
        if (findingDialog!=null)
        {
            if (findingDialog.isShowing())
            {
                findingDialog.dismiss();
            }
        }
    }

}
